﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace CProd
{
    public partial class frmUtilizadores : Form
    {
        #region  Declarações

        SqlDataAdapter daUsers = new SqlDataAdapter();
        SqlCommandBuilder Builder = new SqlCommandBuilder();
        DataTable dtUsers = new DataTable("dtUsers");
        DataView dvUsers = new DataView();
        BindingSource srcUsers = new BindingSource();
        Boolean newRecord = false;

        #endregion

        public frmUtilizadores()
        {
            InitializeComponent();
        }

        private void frmUsers_Load(object sender, EventArgs e)
        {
            this.Builder.DataAdapter = this.daUsers;
            this.daUsers.SelectCommand = new SqlCommand("Select * from Utilizadores", frmEntrada.Conn);
            this.daUsers.UpdateCommand = Builder.GetUpdateCommand();
            this.dtUsers.CaseSensitive = true;
            this.daUsers.Fill(this.dtUsers);

            this.srcUsers.DataSource = this.dtUsers.DefaultView;
            this.dvUsers.Table = this.dtUsers;

            this.txtCodigo.DataBindings.Add("Text", srcUsers, "Codigo");
            this.txtNome.DataBindings.Add("Text", srcUsers, "Nome");

            this.GridUsers.DataSource = this.srcUsers;            
            this.GridUsers.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            this.GridUsers.Columns[0].HeaderText = "Código";
            this.GridUsers.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
        }

        private void btnSair_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        private void btFirst_Click(object sender, EventArgs e)
        { this.srcUsers.MoveFirst(); }

        private void btLast_Click(object sender, EventArgs e)
        { this.srcUsers.MoveLast(); }

        private void btNext_Click(object sender, EventArgs e)
        { this.srcUsers.MoveNext(); }

        private void btPrev_Click(object sender, EventArgs e)
        { this.srcUsers.MovePrevious(); }

        private void btInsert_Click(object sender, EventArgs e)
        {
            this.txtCodigo.ReadOnly = false;
            this.txtCodigo.Text = "";
            this.txtNome.Text = "";
            this.txtCodigo.Focus();
            this.newRecord = true;
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            if (this.txtCodigo.ReadOnly == false) this.txtCodigo.ReadOnly = true;

            try
            {
                #region novo registo

                if (this.newRecord == true)
                {                    
                    this.dvUsers.RowFilter =string .Format( "Codigo='{0}'", this.txtCodigo.Text);
                    if (this.dvUsers.Count > 0)
                    {
                        MessageBox.Show("Já existe esse utilizador");
                        this.newRecord = false;
                    }
                    else
                    {
                        DataRow dRow = this.dtUsers.NewRow();
                        dRow[0] = this.txtCodigo.Text;
                        dRow[1] = this.txtNome.Text;
                        this.dtUsers.Rows.Add(dRow);
                    }
                }
                #endregion
                else
                {
                    this.BindingContext[this.srcUsers].EndCurrentEdit();
                }
                this.daUsers.Update(this.dtUsers);

                //Actualiza Dados Mostrados
                this.dtUsers.Clear();
                this.daUsers.Fill(this.dtUsers);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Tem a certeza?", "Apagar Registo", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                try
                {
                    this.srcUsers.RemoveCurrent();
                    this.daUsers.Update(this.dtUsers);
                    MessageBox.Show("Registo Apagado com Sucesso");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }
}
